//
//  ViewController.m
//  helloword
//
//  Created by chen on 17/2/15.
//  Copyright © 2017年 chen. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITextViewDelegate,UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UISwitch *leftSwitch;
@property (strong, nonatomic) IBOutlet UISwitch *rightSwitch;
@property (strong, nonatomic) IBOutlet UILabel *sliderValue;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化sliderValue值
    self.sliderValue.text = @"50";
    //打开键盘通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    //隐藏键盘通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
    // Do any additional setup after loading the view, typically from a nib.
}
- (void)viewWillDisappear:(BOOL)animated{
    //注销键盘出现通知
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
    [super viewWillDisappear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark -- UISwitch控件
- (IBAction)switchValueChanged:(id)sender {
    UISwitch *witchSwitch = (UISwitch *)sender;
    BOOL setting = witchSwitch.isOn;
    [_leftSwitch setOn:setting animated:TRUE];
    [_rightSwitch setOn:setting animated:TRUE];
}
#pragma mark -- Segments控件
- (IBAction)touchDown:(id)sender {
    UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;

    NSLog(@"选择的段：%li",segmentedControl.selectedSegmentIndex);
    if (self.leftSwitch.hidden) {
        self.rightSwitch.hidden = FALSE;
        self.leftSwitch.hidden = FALSE;
    }else{
        self.leftSwitch.hidden = TRUE;
        self.rightSwitch.hidden = TRUE;
    }
}
#pragma mark -- Slider控件

- (IBAction)sliderValueChange:(id)sender {
    UISlider *slider = (UISlider *)sender;
    int progressAsInt = (int)slider.value;
    NSString *newText = [[NSString alloc] initWithFormat:@"%d",progressAsInt];
    self.sliderValue.text = newText;
}


#pragma mark -- UITextViewDelegate委托协议方法
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]) {
        NSLog(@"TextView获取焦点，点击return建");
        [textView resignFirstResponder];
        return FALSE;
    }
    return TRUE;
}
- (void)keyboardDidHide:(NSNotification *)notif{
    NSLog(@"键盘关闭");
}
- (void)keyboardDidShow:(NSNotification *)notif{
    NSLog(@"键盘打开");
}
#pragma mark -- UITextFieldDelegate委托协议方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    NSLog(@"TextField获取焦点，点击return建");
    [textField resignFirstResponder];
    return TRUE;
}

@end
